//
//  HMStatusCell.swift
//  Weibo
//
//  Created by 米易雅克 on 16/4/19.
//  Copyright © 2016年 米易雅克. All rights reserved.
//

import UIKit
import SnapKit
//自定义微博cell
class HMStatusCell: UITableViewCell {

    //记录之前的bottomview的top约束
    var bottomViewTopConstraint: Constraint?
    
    var status: HMStatus? {
        didSet {
        // 当控制器给cell设置模型后,来做操作
        // 将微博模型传递给originalView
        originalView.status = status
        
        //先卸掉之前的bottom View的顶部约束
        bottomViewTopConstraint?.uninstall()
        //如果有被转发的微博，将被转发的微博设置给被转发的view
            if let reweetStatus = status?.retweeted_status {
                reweetView.hidden = false
                reweetView.retweetStatus = reweetStatus
                
                //有被转发的微博的时候bottomView的顶部参照被转发View的底部
                bottomView.snp_makeConstraints(closure: { (make) in
                    bottomViewTopConstraint = make.top.equalTo(reweetView.snp_bottom).constraint
                })
            } else {
               //如果没有被转发微博的view，将被转发的view隐藏
               //虽然reweetView被隐藏了但是bottomView的顶部约束还是参照 被转发的View 的底部
                reweetView.hidden = true
                
               //将bottomView的顶部约束参照物改为原创view的底部
                
                bottomView.snp_makeConstraints(closure: { (make) in
                    bottomViewTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    
    }
   override init(style: UITableViewCellStyle, reuseIdentifier reuseIndentifier: String?) {
    
        super.init(style: style, reuseIdentifier: reuseIndentifier)
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func prepareUI() {
    
        //添加控件添加
        contentView.addSubview(originalView)
        contentView.addSubview(reweetView)
        contentView.addSubview(bottomView)
        
        //添加约束
        originalView.snp_makeConstraints { (make) in
            
            make.leading.trailing.equalTo(contentView)
            //顶部偏移10
            make.top.equalTo(contentView).offset(10)
        
        }
        //被转发view
        reweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        
        bottomView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
//            make.top.equalTo(originalView.snp_bottom)
            bottomViewTopConstraint = make.top.equalTo(reweetView.snp_bottom).constraint
            make.height.equalTo(37)
        }
    //设置contenview的约束
        contentView.snp_makeConstraints { (make) in
            make.trailing.leading.top.equalTo(self)
            //底部跟随最下面子控件底部
            make.bottom.equalTo(bottomView)
        }
    
    
    }
//MARK: 懒加载
    //原创view
    private lazy var originalView: HMStatusOriginalView = HMStatusOriginalView()
    //底部view
    private lazy var bottomView: HMStatusBottomView = HMStatusBottomView()
    //被转发的view
    private lazy var reweetView: HMStatusRetweetView = HMStatusRetweetView()
}
